﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using ERP.ERPSystem.ProductionSystem;

namespace ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 班组控制器
    /// </summary>
    public class TeamController : Controller
    {
        private readonly ITeamService _teamService;

        public TeamController(ITeamService teamService)
        {
            _teamService = teamService;
        }

        /// <summary>
        /// 班组管理页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取班组分页列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">每页数量</param>
        /// <param name="teamCode">班组编号</param>
        /// <param name="teamName">班组名称</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTeamPageList(int page = 1, int limit = 10, string teamCode = null, string teamName = null)
        {
            try
            {
                var result = await _teamService.GetTeamPageListAsync(page, limit, teamCode, teamName);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = $"查询失败：{ex.Message}",
                    count = 0,
                    data = new object[] { }
                });
            }
        }

        /// <summary>
        /// 获取班组详情
        /// </summary>
        /// <param name="id">班组ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTeamById(int id)
        {
            try
            {
                var result = await _teamService.GetTeamByIdAsync(id);
                if (result != null)
                {
                    return Json(new { success = true, data = result });
                }
                else
                {
                    return Json(new { success = false, message = "班组不存在" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = $"获取详情失败：{ex.Message}" });
            }
        }
    }
}
